package com.maikang.exammall.repository;

import com.maikang.exammall.entity.Order;
import com.maikang.exammall.entity.dto.order.OrderWithUserDTO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

//import com.maikang.exammall.entity.dto.order.OrderDetailDTO;


@Repository
public interface OrderRepository extends BaseRepository<Order, Long>, JpaSpecificationExecutor<Order> {
    Order findByIdAndUserIdAndInvisibleToUser(Long id, Long userId, boolean invisibleToUser);

    Optional<Order> findByIdAndUserIdAndInvisibleToUserAndStatus(Long id, Long userId, boolean invisibleToUser, Integer status);

    Order findByOutTradeNo(String outTradeNo);

    Optional<Order> findByIdAndUserId(Long id, Long userId);


    @Query("UPDATE Order ordr SET ordr.status = :status WHERE ordr.id IN :ids")
    @Modifying
    void updateStatusByIds(@Param("status") Integer status, @Param("ids") List<Long> ids);


    @Query("SELECT new com.maikang.exammall.entity.dto.order.OrderWithUserDTO(odr.id, " +
            "odr.outTradeNo, odr.createdDate, odr.userId, user.username, user.avatarUrl) " +
            "FROM Order odr " +
            "INNER JOIN User user " +
            "ON odr.userId = user.id AND user.isDeleted = 0 " +
            "WHERE odr.id IN (:ids) ")
    List<OrderWithUserDTO> findOrderWithUserByIds(@Param("ids") Iterable<Long> ids);

    Integer countByStatusAndUserId(Integer status, Long userId);


//    @Query("SELECT new com.maikang.exammall.entity.dto.OrderItemBriefDTO(" +
//            "orderItem.pkgAvatarUrl, orderItem.pkgName, orderItem.pkgPrice, " +
//            "orderItem.pkgId, order.userId, order.id, order.outTradeNo, order.status, orderItem.hospitalName," +
//            "order.amount, order.createdDate) " +
//            "FROM Order order " +
//            "INNER JOIN OrderItem orderItem ON order.id = orderItem.orderId AND orderItem.isDeleted = 0 " +
//            "WHERE order.userId = :useId")
//    Slice<OrderListOutputDTO> findByUserId(@Param("userId")Long userId, Pageable pageable);
//
//    @Query("SELECT new com.maikang.exammall.entity.dto.OrderItemBriefDTO(" +
//            "orderItem.pkgAvatarUrl, orderItem.pkgName, orderItem.pkgPrice, " +
//            "orderItem.pkgId, order.userId, order.id, order.outTradeNo, order.status, orderItem.hospitalName," +
//            "order.amount, order.createdDate) " +
//            "FROM Order order " +
//            "INNER JOIN OrderItem orderItem ON order.id = orderItem.orderId AND orderItem.isDeleted = 0 " +
//            "WHERE order.userId = :useId AND order.status = :status")
//    Slice<OrderListOutputDTO> findByUserIdAndStatus(@Param("userId")Long userId, @Param("status")Integer status, Pageable pageable);
//
//    @Query("SELECT new com.maikang.exammall.entity.dto.OrderItemBriefDTO(" +
//            "orderItem.pkgAvatarUrl, orderItem.pkgName, orderItem.pkgPrice, " +
//            "orderItem.pkgId, order.userId, order.id, order.outTradeNo, order.status, orderItem.hospitalName," +
//            "order.amount, order.createdDate) " +
//            "FROM Order order " +
//            "INNER JOIN OrderItem orderItem ON order.id = orderItem.orderId AND orderItem.isDeleted = 0 " +
//            "WHERE order.userId = :useId AND (orderItem.hospitalName like :name OR orderItem.pkgName like :name)")
//    Slice<OrderListOutputDTO> findByUserIdAndName(@Param("userId")Long userId, @Param("name")String name, Pageable pageable);

//    void deleteByIdAndUserId(Long id, Long userId);
}
